package by.grsu.samusev.page.pages;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.DriverDao;
import by.grsu.samusev.entity.Driver;

public class DriverListPage extends WebPage{
	public DriverListPage() {
		super();
		
		final DriverDao driverDao = new DriverDao(SamusevWebApplication.DATA_XML_FOLDER);
		final List<Driver> drivers = driverDao.getAll();
		
		add(new DataView<Driver>("table-rows", new ListDataProvider<Driver>(drivers)) {
			@Override
			protected void populateItem(final Item<Driver> item) {
				final Driver driver = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("id", driver.getId()));
				item.add(new Label("name", driver.getName()));
				item.add(new Label("CarId", driver.getCarId()));				

				// add button component to open "edit" page
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new DriverEditPage(driver));
					}
				});

				// add button component to delete Circle
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						// execute "delete" operation
						driverDao.delete(driver.getId());
						// reload page
						setResponsePage(new DriverListPage());
					}
				});
			}
		});
		
	}
}
